/**
 * Place holder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gde.internal.ui.block;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.ui.StringVariableSelectionDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

import com.softao.ming.gde.internal.core.launcher.LaunchUtil;
import com.softao.ming.gde.internal.ui.util.SWTUtil;

/**
 * 
 */
public class LocationTextBlock implements IUIBlock{
	class Listener extends SelectionAdapter implements ModifyListener {
		public void widgetSelected(SelectionEvent e) {
			Object source = e.getSource();
			if (source == mFileSystemButton) {
				handleBrowseFileSystem();
			} else {
			}
		}

		public void modifyText(ModifyEvent e) {
		}
	}
	
	private Shell mShell;
	private Button mFileSystemButton;
	private Text mLocationText;
	private Link mLocationLink;
	private Listener mListener = new Listener();

	@Override
	public void createControl(Composite parent) {
		mShell = parent.getShell();
		
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		container.setLayout(layout);
		container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		mLocationLink = new Link(container, SWT.NONE);
		mLocationLink.setText("Location:");
		
		mLocationText = new Text(container, SWT.SINGLE | SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		gd.widthHint = 400;
		mLocationText.addModifyListener(mListener);
		mLocationText.setLayoutData(gd);
		mFileSystemButton = createPushButton(container, "&Browse...");
	}

	private Button createPushButton(Composite parent, String text) {
		Button button = new Button(parent, SWT.PUSH);
		button.setText(text);
		GridData gridData = new GridData();
		button.setLayoutData(gridData);
		button.addSelectionListener(mListener);
		SWTUtil.setButtonDimensionHint(button);
		return button;
	}

	private void handleBrowseFileSystem() {
		DirectoryDialog dialog = new DirectoryDialog(mShell);
		dialog.setFilterPath(getLocation());
		dialog.setText("Directory Selection");
		dialog.setMessage("Choose a directory:");
		String result = dialog.open();
		if (result != null)
			mLocationText.setText(result);
	}

	@SuppressWarnings("unused")
	private void handleBrowseWorkspace() {
		ContainerSelectionDialog dialog = new ContainerSelectionDialog(mShell, getContainer(), true, "Choose a location relative to the workspace:");
		if (dialog.open() == Window.OK) {
			Object[] result = dialog.getResult();
			if (result.length == 0)
				return;
			IPath path = (IPath) result[0];
			mLocationText.setText("${workspace_loc:" + path.makeRelative().toString() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	
	@SuppressWarnings("unused")
	private void handleInsertVariable() {
		StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(mShell);
		if (dialog.open() == Window.OK)
			mLocationText.insert(dialog.getVariableExpression());
	}
	
	private IContainer getContainer() {
		String path = getLocation();
		IContainer container = LaunchUtil.getContainer(path);
		if (container == null) {
			return ResourcesPlugin.getWorkspace().getRoot();
		} else {
			return container;
		}
	}


	/**
	 * @return location.
	 */
	public String getLocation() {
		if (mLocationText != null) {
			return mLocationText.getText().trim();
		} else {
			return null;
		}
	}
	
	/**
	 * @param location
	 */
	public void setLocation(String location) {
		mLocationText.setText(location);
	}
	
	/**
	 * @param text
	 */
	public void setLabel(String text) {
		mLocationLink.setText(text);
	}

	/**
	 * @return the label.
	 */
	public String getLabel() {
		return mLocationLink.getText();
	}
	
	/**
	 * @param listener the modifiedListener to set
	 */
	public void addModifyListener(ModifyListener listener) {
		mLocationText.addModifyListener(listener);
	}
	
	/**
	 * @param listener
	 */
	public void removeModifyListener(ModifyListener listener) {
		mLocationText.removeModifyListener(listener);
	}

}
